package dev.hitools.common.utils.image.select

import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import dev.hitools.common.entries.ImageSelectResult
import java.lang.IllegalStateException

class ImageResultContract : ActivityResultContract<Intent, ImageSelectResult>() {
    private var requestCode = 0
    override fun createIntent(context: Context, input: Intent): Intent {
        requestCode = input.getIntExtra(CODE, -1)
        if (requestCode == -1) throw IllegalStateException("requestCode error ,requestCode = $requestCode")
        return input
    }

    override fun parseResult(resultCode: Int, intent: Intent?): ImageSelectResult {
        return ImageSelectResult(resultCode, requestCode, intent)
    }

    companion object {
        const val CODE = "image_request_code"
    }
}